<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Dcat\Admin\Traits\HasDateTimeFormatter;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
    use HasDateTimeFormatter;
    use Traits\LastActivedAtHelper;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'avatar',
        'sex',
        'notification_count',
        'last_actived_at',
        'phone',
        'weixin_openid',
        'weixin_unionid',
        'weapp_openid',
        'weixin_session_key',
        'remember_token',
        'channel',
        'city',
        'province',
        'city',
        'birthday'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'last_actived_at' => 'datetime',
        'referee_id' => 'integer'
    ];

    public function isAuthorOf($model)
    {
        return $this->id == $model->user_id;
    }

    public function rmore()
    {
        return $this->hasOne(Referee::class);
    }

    //判断用户是否认证
    public function isCertified()
    {
        $result = app('App\Services\DfacService')->getCustomerInfo($this->weixin_unionid);
        //如果未找到用户则认为是未认证用户
        if ($result['errcode'] == 1201) return false;
        return $result['data']['is_bind'];
    }
}
